package com.chenye.service1;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther:chenye
 * @Data:2018/9/5
 * @Description: 这里的消费者要单独从Application启动类里面提取出来，不然无法执行切面
 */
@Component
public class RabbitConsumer {
    @Autowired
    private Tracer tracer;

    @RabbitHandler
    @RabbitListener(queues = {"test.zipkin"})
    public void test(Message message, Channel channel) throws IOException {
        Span span = tracer.getCurrentSpan();
        Span newSpan = tracer.createSpan("qweqwe", span);
        try{
            tracer.addTag("test1","rabbitmq");
            System.err.println("rabbitMq");
        }catch (Exception e){
            System.err.println("error");
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
        tracer.close(newSpan);
    }
}
